import request from '@/utils/request'
import Qs from 'qs'

export function getTasks(size, page, kw, user_id, environment_id, project_id, status) {
    let params = ''
    params += (size ? (params === '' ? '' : '&') + 'size=' + size : '')
    params += (page ? (params === '' ? '' : '&') + 'page=' + page : '')
    params += (kw ? (params === '' ? '' : '&') + 'kw=' + kw : '')
    params += (user_id ? (params === '' ? '' : '&') + 'user_id=' + user_id : '')
    params += (environment_id ? (params === '' ? '' : '&') + 'environment_id=' + environment_id : '')
    params += (project_id ? (params === '' ? '' : '&') + 'project_id=' + project_id : '')
    params += (status ? (params === '' ? '' : '&') + 'status=' + status : '')
    if (params !== '') {
        params = '?' + params
    }
    return request({
        url: '/task/' + params,
        method: 'get'
    })
}

export function getTask(taskid) {
    return request({
        url: '/task/' + taskid,
        method: 'get'
    })
}

export function cteateTask(task, taskid) {
    return request({
        url: '/task/' + (taskid ? taskid : ''),
        method: 'post',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded, multipart/form-data' },
        transformRequest: [function(data) {
            data = Qs.stringify(data)
            return data
        }],
        data: task
    })
}

export function updateTask(taskid, task) {
    return request({
        url: '/task/' + taskid,
        method: 'put',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded, multipart/form-data' },
        transformRequest: [function(data) {
            data = Qs.stringify(data)
            return data
        }],
        data: task
    })
}

export function deleteTask(taskid) {
    return request({
        url: '/task/' + taskid,
        method: 'delete'
    })
}

export function auditTask(taskid) {
    return request({
        url: '/task/' + taskid + '/audit',
        method: 'put'
    })
}

export function rejectTask(taskid) {
    return request({
        url: '/task/' + taskid + '/reject',
        method: 'put'
    })
}

export function rollbackTask(taskid) {
    return request({
        url: '/task/' + taskid + '/rollback',
        method: 'put'
    })
}